package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.Impl.DeptServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
@RequestMapping("/depts")
@RestController
@Slf4j
public class DeptController {
  //  @Autowired                               //  1.1>依赖注入
 //   @Qualifier(value = "deptServiceImpl")    //  1.2>根据bean名称注入对象
    @Resource(name = "deptServiceImpl")      //  2>jdk提供的注解
    private DeptServiceImpl deptService;  //= new DeptServiceImpl();          new对象操作交给了IOC
    @GetMapping          //查询全部
    public Result list() throws IOException {
        log.info("部门列表查询成功");
        List<Dept> list = deptService.list();
        return Result.success(list);
    }
    @Log
    @DeleteMapping       //删除
    public Result deleteById( Integer id )  {
        System.out.println("根据ID删除部门: " + id);
        deptService.deleteById(id);
        return Result.success();
    }
    @Log
    @PostMapping           //新增
    public Result add(@RequestBody Dept dept) {
        System.out.println("添加部门: " + dept);
        deptService.add(dept);
        return Result.success();
    }
    @GetMapping("/{id}")      //根据ID查询部门的数据
    public Result selectById(@PathVariable("id") Integer id) {
        System.out.println("根据ID查询部门数据: " + id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping           //修改
    public Result update(@RequestBody Dept dept) {
        System.out.println("修改部门: " + dept);
        deptService.update(dept);
        return Result.success();
    }
}
